package com.xingchi.tornado.security.enums;

import com.xingchi.tornado.anno.Dict;
import com.xingchi.tornado.basic.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 登录来源枚举
 *
 * @author xingchi
 * @date 2024/9/29 16:58
 */
@Getter
@Dict(value = "LOGIN_SOURCE", desc = "登录来源枚举", name = "登录来源")
public enum LoginSourceEnum implements BaseEnum {

    PC(1, "PC"),
    APP(2, "APP"),
    CHAT_MINI(3, "小程序"),
    H5(4, "H5")

    ;
    private final Integer code;
    private final String desc;

    LoginSourceEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    private static final Map<Integer, LoginSourceEnum> CODE_MAPPINGS = Arrays.stream(LoginSourceEnum.values())
            .collect(Collectors.toMap(LoginSourceEnum::getCode, Function.identity()));

    public static LoginSourceEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static LoginSourceEnum getInstanceOrDefault(Integer code, LoginSourceEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

    public static boolean exists(Integer code) {
        return CODE_MAPPINGS.containsKey(code);
    }

}
